package test_achieve.web;

import constant.Constant;
import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import util.ConfigSql;
import test_achieve.Devices;
import util.JdbcUtil;
import util.tools.BusinessTools;
import util.tools.CommonTools;

import java.net.UnknownHostException;

/**
 * Created by chenkuo on 2017/6/19.
 */
public class Login {
    private String loginCaptcha;


    @Given("^w进入用户登录界面$")
    public void w进入用户登录界面() throws UnknownHostException {
        Constant.driverWeb.get(Constant.loginURL);
        Constant.driverWeb.findElement(By.id("captchaCode2_link")).click();

    }
    @Given("^w打开官网主页面$")
    public void w打开官网主页面() throws Throwable {
        Constant.driverWeb.get(Constant.mainPageURL);
    }

    @When("^w输入用户的账户与密码：(.+)：(.+)$")
    public void putUserNameAndPwd(String userName,String userPassword) throws UnknownHostException {
        System.out.println("用户手机号："+userName);
         Constant.driverWeb.findElement(By.xpath("//input[@id='username']")).sendKeys(userName);
        Constant.driverWeb.findElement(By.xpath("//input[@id='password']")).sendKeys(userPassword);
        w输入登录的验证码();
    }
    @Then("^w输入登录的验证码$")
    public void w输入登录的验证码() throws UnknownHostException {
        try {
            String captcha = BusinessTools.getWebGraphicalCaptcha(Constant.driverWeb,"_pc_login");
            Constant.driverWeb.findElement(By.id("captchaCode2")).sendKeys(captcha);
        }catch (Exception e){

        }

    }
    @Then("^w点击登录按钮$")
    public void clickLogin(){
        Constant.driverWeb.findElements(By.className("form-actions")).get(1).click();
//        Constant.driverWeb.findElement(By.xpath("//*[@id=\"tab2\"]/div/form/div[5]")).click();

    }
    @Then("^w登录成功$")
    public void loginSucess()  {
//        WebEl checklogin = driver.findElement(By.xpath("html/body/div[1]/div[1]/div/div/div/div/a[2]/span[1]"));
//        Assert.assertEquals(execresult,checklogin.getText());

    }

    @Given("^w用户已登录：(.+)$")
    public   void wLoging(String username) throws UnknownHostException {
        BusinessTools.removeLoginNum(username,"pc");
        w进入用户登录界面();
        putUserNameAndPwd(username, "123456");
        clickLogin();
    }

    @Given("^w打开浏览器并登录用户账户：(.+)$")
    public void w打开浏览器并登录用户账户(String username) throws Throwable {
        Devices devices = new Devices();
        devices.openBrowser();
        wLoging( username);
    }

    @Given("^w打开浏览器后登录并获取用户余额：(.+)：(.+)$")
    public void w打开浏览器后登录并获取用户余额(String username, String city) throws Throwable {
        w打开浏览器并登录用户账户(username);
        OrderMain orderMain = new OrderMain();
        orderMain.w进入我的闪送中();
        IncreasePrice increasePrice = new IncreasePrice();
        increasePrice.w获取用户账户余额(city);
    }

    @Given("^w确认登录成功$")
    public void w确认登录成功() throws Throwable {
        boolean checkElement = CommonTools.checkElementExit(Constant.driverWeb,"linkText","我的闪送");
        Assert.assertEquals(true,checkElement);
    }

    @Given("^w点击等级按钮$")
    public void w点击等级按钮() throws Throwable {
        Constant.driverWeb.findElement(By.id("level_view")).click();//点击等级图标
    }

    @Given("^w确认用户等级为：(.+)$")
    public void w确认用户等级为(String grade) throws Throwable {
        String pageString = Constant.driverWeb.findElement(By.xpath("//*[@id=\"#price\"]/div/div/h1")).getText();//等级显示语句
        String pageLevel = CommonTools.cutEnd(pageString,"的等级：");//截取当前等级
        System.out.println("level："+pageLevel);
        Assert.assertEquals(grade,pageLevel);
    }

    @Given("^w确认登录错误提醒：(.+)$")
    public void w确认登录错误提醒(String remind ) throws Throwable {
        String pageRemind = Constant.driverWeb.findElement(By.id("error")).getText();//获取登录错误提醒
//        System.out.println(pageRemind);
        Assert.assertEquals(remind,pageRemind);
    }

    @Given("^w点击退出按钮$")
    public void w点击退出按钮() throws Throwable {
        Constant.driverWeb.findElement(By.xpath("/html/body/div[1]/div[1]/div[1]/div[1]/a[2]/span")).click();
    }

    @Given("^w点击手机动态密码登录按钮$")
    public void w点击手机动态密码登录按钮() throws Throwable {
        Constant.driverWeb.findElement(By.className("icon-mobile-phone")).click();
    }

    @Given("^w动态登录界面，输入用户账户：(.+)$")
    public void w动态登录界面输入用户账户(String user) throws Throwable {
        Constant.driverWeb.findElement(By.id("qusername")).sendKeys(user);
    }

    @Given("^w点击获取短信验证码按钮$")
    public void w点击获取短信验证码按钮() throws Throwable {
        Constant.driverWeb.findElement(By.id("qmobile_pwd_btn")).click();
    }

    @Given("^数据库获取短信验证码：(.+)$")
    public void 数据库获取短信验证码(String num) throws Throwable {
        Thread.sleep(1500);
        String secretNum = BusinessTools.getMobileSecret(num);
        String message = JdbcUtil.sqlString(ConfigSql.get("querySms1Content",secretNum));
        System.out.println("数据库获取短信验证码："+message);
         this.loginCaptcha = CommonTools.cutMiddle(message,"验证码为:",",[有效期");
         Constant.mobileMessage=this.loginCaptcha;
//        String message1 = JdbcUtil.sqlString(ConfigSql.get("querySms2Content",secretNum));
        System.out.println( Constant.mobileMessage);//注册短信验证码，应该是会发送到2库中
    }

    @Given("^w输入短信验证码$")
    public void w输入短信验证码() throws Throwable {
        Constant.driverWeb.findElement(By.id("qpassword")).sendKeys(this.loginCaptcha);
    }

    @Given("^w清空手机号输入框并输入手机号：(.+)$")
    public void w清空手机号输入框并输入手机号(String mobile) throws Throwable {
        WebElement fromMobile = Constant.driverWeb.findElement(By.id("dis_fromMobile"));
        fromMobile.clear();
        fromMobile.sendKeys(mobile);
    }

    @Given("^w提交订单界面，点击获取登录短信验证码按钮$")
    public void w提交订单界面_点击获取登录短信验证码按钮() throws Exception {
        Constant.driverWeb.findElement(By.id("mobile_pwd_btn")).click();
    }

    @Given("^w提交订单界面，输入登录短信验证码$")
    public void w提交订单界面_输入登录短信验证码() throws Exception {
        Constant.driverWeb.findElement(By.id("sms_pass")).sendKeys(this.loginCaptcha);
    }

    @Given("^w已经进入用户注册界面$")
    public void w已经进入用户注册界面() throws Throwable {
        Devices devices = new Devices();
        devices.openBrowser();
        w打开用户注册界面();
    }
    @Given("^w打开用户注册界面$")
    public void w打开用户注册界面() throws Throwable {
        Constant.driverWeb.get(Constant.userRegisterURL);
    }

    @Given("^pduboo接口删除此用户$")
    public void pduboo接口删除此用户() throws Throwable {

    }

    @Given("^w注册界面，输入手机号并点击获取验证码：(.+)$")
    public void w注册界面输入手机号并点击获取验证码(String number) throws Throwable {
        Constant.driverWeb.findElement(By.id("username")).sendKeys(number);//输入手机号码
        Constant.driverWeb.findElement(By.id("register_send_sms")).click();//点击获取验证码按钮
    }

    @Given("^w输入短信验证码与密码：(.+)：(.+)$")
    public void w输入短信验证码与密码密码(String password,String rpassword) throws Throwable {
        System.out.println(this.loginCaptcha);
        Constant.driverWeb.findElement(By.xpath("/html/body/div[2]/form/div[3]/div[1]/input")).sendKeys(this.loginCaptcha);//输入验证码
        w输入新密码和确认密码( password, rpassword);

    }

    @Given("^w点击同意注册按钮$")
    public void w点击同意注册按钮() throws Throwable {
        Constant.driverWeb.findElement(By.id("register-submit-btn")).click();
    }

    @Given("^w确定注册提醒内容：(.+)$")
    public void w确定注册提醒内容(String content) throws Throwable {
        String content1 = Constant.driverWeb.findElement(By.className("help-block")).getText();
        System.out.println("提醒："+content1);
        Assert.assertEquals(content,content1);

    }

    @Given("^w点击忘记密码按钮$")
    public void w点击忘记密码按钮() throws Throwable {
        Constant.driverWeb.findElement(By.id("forget-password")).click();
    }

    @Given("^w忘记密码界面，输入手机号并点击获取验证码：(.+)$")
    public void w忘记密码界面输入手机号并点击获取验证码(String userNum) throws Throwable {
        Constant.driverWeb.findElement(By.id("mobile")).sendKeys(userNum);
        Constant.driverWeb.findElement(By.id("send_checkcode")).click();//点击获取验证码
    }

    @Given("^w忘记密码界面，点击提交按钮$")
    public void w忘记密码界面点击提交按钮() throws Throwable {
        Constant.driverWeb.findElement(By.xpath("/html/body/div[2]/form/div[4]/button")).click();

    }

    @Given("^w输入新密码和确认密码：(.+)：(.+)$")
    public void w输入新密码和确认密码(String password,String rpassword) throws Exception {
        Constant.driverWeb.findElement(By.id("password")).sendKeys(password);
        Constant.driverWeb.findElement(By.id("rpassword")).sendKeys(rpassword);
    }

    @Given("^w点击重置密码按钮$")
    public void w点击重置密码按钮() throws Exception {
        Constant.driverWeb.findElement(By.id("register-submit-btn")).click();
    }

    @Given("^w提示您已经成功重置密码$")
    public void w提示您已经成功重置密码() throws Throwable {
        String content =Constant.driverWeb.findElement(By.className("color-green")).getText();
        System.out.println("提醒："+content);
        Assert.assertEquals(content,"欢迎您，! 您已经成功重置密码，赶紧去体验闪送服务吧！");
    }

    @Given("^w忘记密码界面，输入短信验证码$")
    public void w忘记密码界面输入短信验证码() throws Throwable {
        Constant.driverWeb.findElement(By.xpath("//*[@id=\"capcha_group\"]/div[1]/input")).sendKeys(this.loginCaptcha);
    }

    @Given("^w验证码登陆界面，点击登录按钮$")
    public void w验证码登陆界面点击登录按钮() throws Throwable {
        Constant.driverWeb.findElement(By.xpath("//*[@id=\"tab1\"]/div/form/div[5]/button")).click();
    }

    @Given("^w输验证码登录界面，图形验证码$")
    public void w输验证码登录界面图形验证码() throws Throwable {
        String captcha = BusinessTools.getWebGraphicalCaptcha(Constant.driverWeb,"_pc_smsLogin");
        Constant.driverWeb.findElement(By.id("captchaCode")).sendKeys(captcha);
//        System.out.println("w输验证码登录界面图形验证码:"+captcha);
    }
}
